<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="!dataForm.id ? '新增' : '修改'"
    :visible.sync="visible"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="80px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item label="配置名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="配置名称" />
      </el-form-item>
      <el-form-item label="配置编码" prop="code">
        <el-input v-model="dataForm.code" placeholder="配置编码" />
      </el-form-item>
      <el-form-item label="配置值" prop="value">
        <el-input v-model="dataForm.value" placeholder="配置值" />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-input v-model="dataForm.type" placeholder="类型" />
      </el-form-item>
      <el-form-item label="排序（升序）" prop="sort">
        <el-input v-model="dataForm.sort" placeholder="排序（升序）" />
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input v-model="dataForm.createBy" placeholder="创建人" />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="创建时间" />
      </el-form-item>
      <el-form-item label="更新人" prop="updateBy">
        <el-input v-model="dataForm.updateBy" placeholder="更新人" />
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime">
        <el-input v-model="dataForm.updateTime" placeholder="更新时间" />
      </el-form-item>
      <el-form-item label="是否删除  -1：已删除  0：正常" prop="delFlag">
        <el-input v-model="dataForm.delFlag" placeholder="是否删除  -1：已删除  0：正常" />
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="dataForm.description" placeholder="描述" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: '',
        code: '',
        value: '',
        type: '',
        sort: '',
        createBy: '',
        createTime: '',
        updateBy: '',
        updateTime: '',
        delFlag: '',
        description: ''
      },
      dataRule: {
        name: [
          { required: true, message: '配置名称不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '配置编码不能为空', trigger: 'blur' }
        ],
        value: [
          { required: true, message: '配置值不能为空', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '类型不能为空', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '排序（升序）不能为空', trigger: 'blur' }
        ],
        createBy: [
          { required: true, message: '创建人不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '创建时间不能为空', trigger: 'blur' }
        ],
        updateBy: [
          { required: true, message: '更新人不能为空', trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: '更新时间不能为空', trigger: 'blur' }
        ],
        delFlag: [
          { required: true, message: '是否删除  -1：已删除  0：正常不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/system/config/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.config.name
              this.dataForm.code = data.config.code
              this.dataForm.value = data.config.value
              this.dataForm.type = data.config.type
              this.dataForm.sort = data.config.sort
              this.dataForm.createBy = data.config.createBy
              this.dataForm.createTime = data.config.createTime
              this.dataForm.updateBy = data.config.updateBy
              this.dataForm.updateTime = data.config.updateTime
              this.dataForm.delFlag = data.config.delFlag
              this.dataForm.description = data.config.description
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/system/config/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'name': this.dataForm.name,
              'code': this.dataForm.code,
              'value': this.dataForm.value,
              'type': this.dataForm.type,
              'sort': this.dataForm.sort,
              'createBy': this.dataForm.createBy,
              'createTime': this.dataForm.createTime,
              'updateBy': this.dataForm.updateBy,
              'updateTime': this.dataForm.updateTime,
              'delFlag': this.dataForm.delFlag,
              'description': this.dataForm.description
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
